#!/bin/bash

function usage 
{
	echo -e "Wrong number of parameters!"
	echo -e "Usage:"
	echo -e "	move_module <module name> <source package> <destiny package> [new module name]"
	echo -e "	- <module name>: Name of an existent module."
	echo -e "	- <source package>: The module's original package."
	echo -e "	- <destiny package>: The package that the module will be moved within."
	echo -e "	- optional: [new module name]: A new name for the module."
	echo -e ""
	echo -e "	Example:"
	echo -e "		move_module my_module my.module.package some.module.package some_module"

	exit 1
}

if [ $# -lt 3 ]
then
	usage
fi

new_module_name="${1}"

if [ $# -eq 4 ]
then
	new_module_name="${4}"
fi

source_file=`echo -e "${2}" | sed "s!\.!/!g"`
source_file="${source_file}/${1}.py"
destiny_file=`echo -e "${3}" | sed "s!\.!/!g"`
destiny_file="${destiny_file}/${new_module_name}.py"

if [ ! -e "${source_file}" ]
then
	echo -e "${source_file} not found!"

	exit 1
fi

cp "${source_file}" "${source_file}.bkp" 
remove_module "${1}" "${2}"
create_module "${new_module_name}" "${3}"
mv "${source_file}.bkp" "${destiny_file}"
